# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import MySQLdb

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

# 连接数据库
conn = MySQLdb.connect(
    host="localhost",   # mysql所在的主机的ip
    port=3306,          # mysql的端口号
    user="root",        # mysql的用户名
    password="123456",  # mysql的密码
    db="reptiles",      # 要使用的库名
    charset="utf8",     # 连接中使用的字符集

)
cursor = conn.cursor(MySQLdb.cursors.DictCursor)  # 获取游标对象,里面可以为空,这样写是为了显示全面的信息


class DishonestyPipeline:
    def process_item(self, item, spider):
        list = [item['iname'], item['age'], item['areaName'], item['duty'], item['sexy'], item['caseCode']]
        print("即将存储到数据库的数据")
        print(list)
        try:  # 有重复的则跳过,不进行存储,需要进行判断,重复的话,将名称换一下
            # sql = "insert into day6_dishonesty values (%s,%s,%s,%s,%s,%s)"  为啥这个不行啊
            sql = "insert into savee values (%s,%s,%s,%s,%s,%s)"

            # last = cursor.execute(sql, list)
            cursor.execute(sql, list)
            conn.commit()
        except:
            pass
